/**
 * 预算理程序
 **/
try{
    MUI.toggleAdvancedEffects(location.href);
}
catch(e){
}


/**
 * 初始化MOCHAUI
 **/
window.addEvent('load', function()
{
    //绘制面板
	MUI.myChain=new Chain();
	MUI.myChain.chain
    (
		function(){MUI.Desktop.initialize();},
		function(){MUI.Dock.initialize();},        
		function()
        {
            // 左面板
            new MUI.Column({
                id: 'sideColumn1',
                placement: 'left',
                width: 210,
                resizeLimit: [100, 400]
            });

            // 主面板
            new MUI.Column({
                id: 'mainColumn',
                placement: 'main',
                resizeLimit: [100, 300]
            });

            // 管理树
            new MUI.Panel(
            {
                id: 'files-panel',
                title: '控制面板',
                //contentURL: 'scripts/mochaui/pages/file-view.html',
                contentURL: 'menu.php',
                column: 'sideColumn1',
                require:
                {

                    css: [MUI.path.plugins + 'tree/css/style.css'],
                    js: [MUI.path.plugins + 'tree/scripts/tree.js'],
                    onload: function(){
                        if (buildTree) buildTree('tree1');
                        //alert(MUI.path.plugins + 'tree/css/style.css');
                        
                    }
                },
                onContentLoaded: function(){
                },
                footerToolbox: true,
                footerToolboxURL: 'menu.footer.php'
            });

            // 初始化主面板内容
            new MUI.Panel(
            {
                id: 'mainPanel',
                title: '资讯版块-管理信息',
                loadMethod:'iframe',
                //contentURL: 'modules/_demo/form.php',
                contentURL: 'modules/news/list.php',
                column: 'mainColumn'
            });
        },
        function(){initializeWindows();}
	).callChain();

    //绑定切换主题控件
    $('themeControl').addEvent('change',function()
    {
        MochaUI.Themes.init(this.options[this.selectedIndex].value),Main.SwitchThemes(this.options[this.selectedIndex].value);
    });

    /**
     *  
     */
    $('UpdatePassword').addEvent('click',function()
    {
        new MUI.Window({
            id: 'EditPassword',
            title: '修改密码',
            loadMethod:'iframe',
            contentURL: 'password.php',
            width: 287,
            height: 150,
            maximizable:false,
            draggable:true,
            resizable:false,
            scrollbars:false
        });
    });
});



/**
 * 主面板函数
 **/
var Main=
{

    Control:
    {
        FromUrl:function($url,$method)
        {
            MUI.updateContent(
            {
                'element': $('files-panel'),
                'loadMethod':'xhr',
                'url': $url,
                'padding': {
                    top: 8,
                    right: 8,
                    bottom: 8,
                    left: 8
                },
                require: {
                    css: [MUI.path.plugins + 'tree/css/style.css'],
                    js: [MUI.path.plugins + 'tree/scripts/tree.js'],
                    onload: function(){

                        if (buildTree) buildTree('tree1');
                        //alert(MUI.path.plugins + 'tree/css/style.css');
                    }
                }
            });
        }
    },

    Panel:
    {
        //从链接信息更新主面板内容
        FromLink:function($obj,$title,$method)
        {
            try
            {
                MUI.updateContent(
                {
                    'element': $('mainPanel'),
                    'loadMethod':$method||'iframe',
                    'url': $obj.href,
                    'title': $title==undefined ? $obj.innerText||$obj.textContent:$title,
                    'padding': {
                        top: 8,
                        right: 8,
                        bottom: 8,
                        left: 8
                    }
                });
            }
            catch(e)
            {
                alert(e.description);
            }
            return false;
        }
    },

    SwitchThemes:function(value)
    {
        var sub=document.getElementById('mainPanel_iframe').contentWindow;
        var $__CssElement= sub.document.getElementsByTagName('link');

        for (var i = 0, len = $__CssElement.length; i < len; i++)
        {
            var temp=$__CssElement[i];
            temp.href=temp.href.replace(/\/themes\/\w+\//,'/themes/'+value+'/');
        }
    }
};